#ifndef __CHUNKFILE_H__
#define __CHUNKFILE_H__



//TO REMEMBER WHEN USING:

//EITHER a chunk contains ONLY data
//OR it contains ONLY other chunks
//otherwise the scheme breaks...
inline int32 flipID(int32 id)
{
  return ((id>>24)&0xFF) | ((id>>8)&0xFF00) | ((id<<8)&0xFF0000) | ((id<<24)&0xFF000000);
}

class ChunkFile
{
  char *data;
  int pos,eof;
  struct ChunkInfo
  {
    int startLocation;
    int parentStartLocation;
    int parentEOF;
    unsigned int ID;
    int length;
  };
  ChunkInfo stack[8];
  LAMEFile file;
  int numLevels;
  bool read;
  bool didFail;
public:
  ChunkFile(const TCHAR *filename, bool _read);
  ~ChunkFile();

  bool failed()
  {
    return didFail;
  }
  bool descend(int32 id);
  void ascend();

  int  readInt();
  void readInt(int &i)
  {
    i = readInt();
  }
  void writeInt(int i);

  String readString();
  void writeString(String str);

  void readData(void *data, int count);
  void writeData(void *data, int count);
  void seek(int _pos);
  int getPos() { return pos;}
  int currentChunkSize()
  {
    if (numLevels)
      return stack[numLevels-1].length;
    else
      return 0;
  }
};

#endif //__CHUNKFILE_H__
